package com.code2roc.jarmanage.util;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class CommonUtil {

    public static String getBodyString(HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader reader = null;
        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            LogUtil.error(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LogUtil.error(e);
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    LogUtil.error(e);
                }
            }
        }
        return sb.toString();
    }

    public static HashMap getParamMap(HttpServletRequest request) {
        HashMap<String, Object> paramMap = new HashMap<>();
        try {
            String param = getBodyString(request);
            JSONObject obj = JSONObject.parseObject(param);
            for (String key : obj.keySet()) {
                paramMap.put(key, obj.get(key));
            }
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return paramMap;
    }

    public static <T> T getParamEntity(HttpServletRequest request, Class<T> clazz) {
        T entity = null;
        try {
            String param = getBodyString(request);
            entity = JSONObject.parseObject(param, clazz);
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return entity;
    }

    public static String getNewGuid() {
        String dateStr = getDateTagToSecond();
        String randomStr = UUID.randomUUID().toString().substring(0, 36);
        return dateStr + randomStr;
    }

    public static String getDateTagToSecond() {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String tag = df.format(new Date());
        return tag;
    }

    public static String getDateTagToSecond2() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd@HH-mm-ss");
        String tag = df.format(new Date());
        return tag;
    }

    public static String getDateFormatToSecond() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tag = df.format(new Date());
        return tag;
    }

    public static String getDateFormatToDay() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String tag = df.format(new Date());
        return tag;
    }

    public static String getRooptPath(HttpServletRequest request) {
        String requestURL = request.getRequestURL().toString();
        String scheme = request.getScheme();
        String servaerName = request.getServerName();
        int port = request.getServerPort();
        if (port == 443) {
            scheme = "https";
        }
        String rootPageURL = scheme + ":" + "//" + servaerName + ":" + port + ConfigUtil.getConfigValue("contextpath");
        if (!rootPageURL.endsWith("/")) {
            rootPageURL += "/";
        }
        return rootPageURL;
    }
}
